package sms.service;

import sms.persistence.IPersistenceDao;

import java.util.Optional;
import java.util.ServiceLoader;

/**
 * 通过java自带的serviceloader查找IPersistenceDao的具体实现
 */
public class IPersistenceDaoLoader {
    public static IPersistenceDao persistenceDao;

    static {
        //通过ServiceLoader的load方法指定获取所有IPersistenceDao实现类的流，取第一个作为IPersistenceDao的实现
        final Optional<IPersistenceDao> optionalDao = ServiceLoader.load(IPersistenceDao.class).findFirst();
        if (optionalDao.isPresent()) {
            persistenceDao = optionalDao.get();
        } else {
            throw new RuntimeException("no IPersistenceDao implement class exist!");
        }
    }
}
